/**
 * 
 */
package com.momosw.games.jericho.board;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.momosw.games.engine.events.Event;
import com.momosw.games.engine.player.Player;

/**
 * <b>Project:</b> JerichoCardGame<br />
 * <b>Package:</b> com.momosw.games.jericho.board<br />
 * <b>Class:</b> Scoring.java<br />
 * <br />
 * <i>GSI 2011</i><br />
 *
 * @author Miguel Coronado (miguelcb84@gmail.com)
 * @version	Aug 21, 2011
 *
 */
public class Scoring implements Event{

    /**
     * The map to store the amount of cards earned by each player.
     * The number of cards is equivalent to the number of points
     * each player scores.
     * The key identifies each player.
     * The value is a List with the cards each player earned during 
     * the scoring phase. It is not a Set because sets do not support
     * duplicate items
     */
    Map <Player, List<Card>> score;
    
    /**
     * Constructor that initializes the number of players
     * @param players
     */
    public Scoring (Collection <Player> players){
        this.score = new HashMap<Player, List<Card>>();
        for (Player player : players){
            this.score.put(player, new LinkedList<Card>());
        }
    }
    
    /**
     * Add a card to the score of the given player
     * @param player    The Player
     * @param card      The Card to add
     */
    public void addCard (Player player, Card card) {
        if (score.containsKey(player)){
            score.get(player).add(card);
            return;
        }
        // There is no scoring for the current player yet
        List<Card> auxSet = new LinkedList<Card>();
        auxSet.add(card);
        score.put(player, auxSet);
    }
    
    /**
     * This prints the results
     */
    public String toString() {
        return score.toString();
    }
    
    /**
     * 
     * @return
     */
    public String getScoringSummary() {
        String res = "";
        for (Player player : score.keySet()) {
            res += ">" +  player.getName() + ": +" + score.get(player).size() + " puntos en esta fase " + score.get(player) + "\r\n";
        }
        return res;
    }
    
    /**
     * This counts the amount of cards for each player and compiles the 
     * results of the scoring 
     * @return  Map - with the results for each player
     */
    public Map<Player, Integer> getResults () {
        Map<Player, Integer> results =  new HashMap<Player, Integer>();
        for (Player player : this.score.keySet()) {
            results.put(player, this.score.get(player).size());
        }
        return results;
    }

    public Event hidePrivate() {
        return this;
    }
    
}